package com.molyfun.parents.activity.report;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.activity.report.adapter.ReportFeedbackListAdapter;
import com.molyfun.parents.rest.bean.ReportFeedbackListResponse;
import com.molyfun.parents.utils.Const;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:12/13/16.
 */
public class ReportFeedbackListActivity extends BaseActivity implements View.OnClickListener {

    private static final String FLAG_TIME_INTERVAL = "time_interval";

    @InjectView(R.id.base_title_left)
    ImageView mTitleBack;
    @InjectView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @InjectView(R.id.empty_layout)
    LinearLayout mEmptyLayout;
    @InjectView(R.id.base_title)
    TextView mTitle;
    @InjectView(R.id.time_interval)
    TextView mTimeInterval;
    LinearLayoutManager mLinearLayoutManager;
    ReportFeedbackListAdapter reportFeedbackListAdapter;

    private String mReportId;
    private String mTimeInteval;

    public static void startActivity(Context context, String reportId, String timeInterval) {
        Intent intent = new Intent();
        intent.putExtra(Const.REPORT_ID, reportId);
        intent.putExtra(FLAG_TIME_INTERVAL, timeInterval);
        intent.setClass(context, ReportFeedbackListActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report_feedbacklist);
        ButterKnife.inject(this);

        mTitleBack.setOnClickListener(this);

        getSupportActionBar().setTitle("");
        mTitle.setText("查看反馈");

        mRecyclerView.setHasFixedSize(true);
        mLinearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLinearLayoutManager);

        mTitleBack.setOnClickListener(this);

        reportFeedbackListAdapter = new ReportFeedbackListAdapter(this, getSupportFragmentManager());
        mRecyclerView.setAdapter(reportFeedbackListAdapter);
        mReportId = getIntent().getStringExtra(Const.REPORT_ID);
        mTimeInteval = getIntent().getStringExtra(FLAG_TIME_INTERVAL);
        mTimeInterval.setText(mTimeInteval);
//        getReportDetail(mReportId);
    }

    @Override
    protected void onResume() {
        getReportDetail(mReportId);
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_left:
                finish();
                break;
        }
    }

    private void getReportDetail(String reportId) {
        showProgressDialog();
        Call<ReportFeedbackListResponse> call = MFPApplication.getInstance().getRestClient().getReportFeedbackList(reportId);
        call.enqueue(new Callback<ReportFeedbackListResponse>() {
            @Override
            public void onResponse(Call<ReportFeedbackListResponse> call, Response<ReportFeedbackListResponse> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getData() != null && response.body().getData().getRecords() != null
                        && response.body().getData().getRecords().size() > 0) {
                    if (response.body().getData().getRecords() == null || response.body().getData().getRecords().size() == 0) {
                        mEmptyLayout.setVisibility(View.VISIBLE);
                    } else {
                        reportFeedbackListAdapter.initData(response.body().getData().getRecords(), mTimeInteval);
                    }
                } else {
                    mEmptyLayout.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onFailure(Call<ReportFeedbackListResponse> call, Throwable t) {
                dismissProgressDialog();
            }
        });
    }

}
